package com.thf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thf.dao.NewsDao;
import com.thf.model.ResultData;
import com.thf.model.news.NewModel;
import com.thf.service.NewsService;
import com.thf.util.ResultTool;
import com.thf.util.Tool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsDao newsDao ;

    @Override
    public ResultData newsFindAll(Map map ) {
        NewModel newsModel = new NewModel();
        newsModel.setName(Tool.jsonToString(map.get("name")));
        newsModel.setCreateTime(Tool.jsonToString(map.get("start")));
        newsModel.setUpdateTime(Tool.jsonToString(map.get("end")));
        newsModel.setType(Tool.jsonToString(map.get("type")));
        newsModel.setEnable(Tool.jsonToString(map.get("enable")));
        List<NewModel> list = null;
        PageHelper.startPage( (int) map.get("startPage"),(int) map.get("pageSize"));
//        list = newsDao.findAll(pageModel, newsModel);
//        list = newsDao.findAll(newsModel);
        list = newsDao.findAll(newsModel);
        HashMap hashMap = new HashMap();
        PageInfo page = new PageInfo(list);
        if (list == null) {
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ERROR_MSG);
        }
        hashMap.put("list",list);
        hashMap.put("count",page.getTotal());
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_SUCCES_MSG, hashMap);
    }

    @Override
    public Integer getCount(Map map) {
        NewModel newsModel = new NewModel();
        newsModel.setName(Tool.jsonToString(map.get("name")));
        newsModel.setCreateTime(Tool.jsonToString(map.get("start")));
        newsModel.setUpdateTime(Tool.jsonToString(map.get("end")));
        newsModel.setType(Tool.jsonToString(map.get("type")));
        newsModel.setEnable(Tool.jsonToString(map.get("enable")));
        Integer count = null ;
        if(newsModel != null){
            count =  newsDao.getCount(newsModel);
        }
        return count;
    }

    @Override
    public ResultData add(Map map) {
        NewModel newModel = new NewModel();
        newModel.setName(Tool.jsonToString(map.get("name")));
        newModel.setType(Tool.jsonToString(map.get("type")));
        newModel.setEnable(Tool.jsonToString(map.get("enable")));
        newModel.setImg(Tool.jsonToString(map.get("url")));
        newModel.setContent(Tool.jsonToString(map.get("describe")));
        newModel.setTitle(Tool.jsonToString(map.get("title")));
        newModel.setLink(Tool.jsonToString(map.get("href")));
        newModel.setReleaseTime(Tool.jsonToString(map.get("releaseTime")));
        newModel.setCreateTime(Tool.dateToString(new Date()));
        Integer count = null ;
        if(newModel!=null){
            count = newsDao.add(newModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ADD_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_ADD_SUCCES_MSG,count);
    }

    @Override
    public ResultData getNewsById(Integer nid) {
        NewModel newModel= null;
        if(nid != null){
            newModel = newsDao.getNewsById(nid);
        }
        if(newModel == null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_NEW_ID_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_NEW_ID_SUCCES_MSG,newModel);
    }

    @Override
    public ResultData update(Map map) {
        NewModel newModel = new NewModel();
        newModel.setName(Tool.jsonToString(map.get("name")));
        newModel.setType(Tool.jsonToString(map.get("type")));
        newModel.setEnable(Tool.jsonToString(map.get("enable")));
        newModel.setImg(Tool.jsonToString(map.get("url")));
        newModel.setContent(Tool.jsonToString(map.get("describe")));
        newModel.setTitle(Tool.jsonToString(map.get("title")));
        newModel.setLink(Tool.jsonToString(map.get("href")));
        newModel.setReleaseTime(Tool.jsonToString(map.get("releaseTime")));
        newModel.setId((Integer) map.get("id"));
        newModel.setUpdateTime(Tool.dateToString(new Date()));
        Integer count = null ;
        if(newModel!=null){
            count = newsDao.update(newModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_UPDATE_ERROR_MSG );
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_UPDATE_SUCCES_MSG,count);
    }

    @Override
    public ResultData delete(Integer nid) {
        Integer count = null ;
        if(nid != null){
            count = newsDao.delete(nid);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_DEL_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_DEL_SUCCES_MSG,count);
    }
}
